from modelscope_agent.tools.base import BaseTool
from modelscope_agent.tools import register_tool
import requests
import json
import os
from typing import Dict, Optional

@register_tool('device-info')
class DeviceInfoTool(BaseTool):
    description = '获取设备的详细信息，输入设备的名称，返回设备的详细信息'
    name = 'device-info'
    parameters: list = [{
        'name': 'dev-name',
        'description': '描述设备的名称，如`火焰切割设备3`',
        'required': True,
        'type': 'string'
        },
    ]
    def __init__(self, cfg: Optional[Dict] = None):
        super().__init__(cfg)

        self.url = cfg['url']
        self.token = cfg['Authorization']
    
    def call(self, params: str, **kwargs):
        # api_url = 'http://172.20.0.72:33011/eam/euipment/eqpAccount/listPage'
        params = self._verify_args(params)
        params = {
            "pageNumber": 1,
            "pageSize": 20,
            "advQueryParam": [
                {
                "value": [str(params['dev-name']),],
                "express": "like",
                "field": "accountName"
                },
            ]
        }
        # data = json.dumps(params)
        # data = f'{{"input": {data}}}'
        headers = { 
            'Authorization': self.token
        }
        response = requests.request('POST', self.url, json=params, headers=headers)
        res = str(json.loads(response.content.decode('utf-8')))
        return res

